﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RandomNumberGeneratorHTML
{
    public partial class RandomNumberGenerator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblErrorMessage.InnerText = string.Empty;
            this.btnGenerate.ServerClick += new EventHandler(btnGenerate_Click);
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                int startNum = Int32.Parse(this.txtStartNum.Value);
                int endNum = Int32.Parse(this.txtEndNum.Value);
                if (startNum > endNum)
                {
                    throw new Exception("The start number should be smaller than the end number");
                }

                int generatedNum = new Random().Next(startNum, endNum);
                this.txtGeneratedNum.Value = generatedNum.ToString();
            }
            catch (Exception ex)
            {
                this.txtGeneratedNum.Value = string.Empty;
                this.lblErrorMessage.InnerText = ex.Message;
            }
        }
    }
}